/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package ormsamples;

import java.util.Random;

import org.orm.*;
public class CreateCoordenadaData {
	public void createTestData() throws PersistentException {
		PersistentTransaction t = orm.CoordenadaPersistentManager.instance().getSession().beginTransaction();
		try {
			
			
			orm.DAOFactory lDAOFactory = orm.DAOFactory.getDAOFactory();
			orm.dao.UsuarioDAO lormUsuarioDAO = lDAOFactory.getUsuarioDAO();
			orm.Usuario lormUsuario = lormUsuarioDAO.createUsuario();
//			 TODO Initialize the properties of the persistent object here, the following properties must be initialized before saving : tarjeta, rut, passwords, nick, nombre
			//lleno las 3 tablas
			lormUsuario.setNombre("zozio");
			lormUsuario.setPass("12345678");
			lormUsuario.setRut("153658540");
			lormUsuarioDAO.save(lormUsuario);
			
			orm.dao.TarjetaDAO lormTarjetaDAO = lDAOFactory.getTarjetaDAO();
			orm.Tarjeta lormTarjeta = lormTarjetaDAO.createTarjeta();
			lormTarjeta.setCod_tarjeta(12345678);
			lormTarjeta.setUsuarioid_usuario(lormUsuario);
			// TODO Initialize the properties of the persistent object here, the following properties must be initialized before saving : cordenada1, usuarioid_usuario, cod_tarjeta
			lormTarjetaDAO.save(lormTarjeta);

			orm.dao.CoordenadaDAO lormCoordenadaDAO = lDAOFactory.getCoordenadaDAO();
			orm.Coordenada []lormCoordenada = new orm.Coordenada[50] ;
			String []a = {"A1","A2","A3","A4","A5","B1","B2","B3","B4","B5","C1","C2","C3","C4","C5","D1","D2","D3","D4","D5","E1","E2","E3","E4","E5","F1","F2","F3","F4","F5","G1","G2","G3","G4","G5","H1","H2","H3","H4","H5","I1","I2","I3","I4","I5","J1","J2","J3","J4","J5"};
			//genero las 50 claves de las coordenadas sin repetirse
			int n = 50;
			int k = n;
			int[] resultado = new int[n];
			int[] numeros=new int[n];       
			Random rnd = new Random();
			int res;
			       
			for(int j=0;j<n;j++){
			    numeros[j]=j+1;
			}
			       
			for(int j=0;j<n;j++){
			    res = rnd.nextInt(k);           
			        resultado[j]=numeros[res];
			        numeros[res]=numeros[k-1];
			        k--;
			}
			
			for(int i = 0;i<50 ;i++){	
				lormCoordenada[i]= lormCoordenadaDAO.createCoordenada();
				lormCoordenada[i].setClave(a[i]);

				
				lormCoordenada[i].setValor(resultado[i]);
				lormCoordenada[i].setTarjetaid_tarjeta(lormTarjeta);
				lormCoordenadaDAO.save(lormCoordenada[i]);
			}
			// TODO Initialize the properties of the persistent object here, the following properties must be initialized before saving : tarjetaid_tarjeta, valor, clave
			t.commit();
		}
		catch (Exception e) {
			t.rollback();
		}
		
	}
	
	public static void main(String[] args) {
		try {
			CreateCoordenadaData createCoordenadaData = new CreateCoordenadaData();
			try {
				createCoordenadaData.createTestData();
			}
			finally {
				orm.CoordenadaPersistentManager.instance().disposePersistentManager();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
